package cn.jetpiece.cloud.system.dp.app.modular.biz.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.jetpiece.cloud.core.page.PageFactory;
import cn.jetpiece.cloud.core.query.QueryGenerator;
import cn.jetpiece.cloud.core.util.HttpContext;
import cn.jetpiece.cloud.core.util.MD5Util;
import cn.jetpiece.cloud.model.page.PageResult;
import cn.jetpiece.cloud.model.response.Result;
import cn.jetpiece.cloud.starter.app.modular.base.controller.BaseController;
import cn.jetpiece.cloud.system.dp.api.biz.DpTApiApi;
import cn.jetpiece.cloud.system.dp.api.biz.model.params.CreateDpTApiParam;
import cn.jetpiece.cloud.system.dp.api.biz.model.params.UpdateDpTApiParam;
import cn.jetpiece.cloud.system.dp.api.biz.model.query.DpTApiQuery;
import cn.jetpiece.cloud.system.dp.api.biz.model.result.DpTApiResult;
import cn.jetpiece.cloud.system.dp.app.enums.DataStatusEnum;
import cn.jetpiece.cloud.system.dp.app.modular.biz.entity.DpTApi;
import cn.jetpiece.cloud.system.dp.app.modular.biz.entity.DpTScopeData;
import cn.jetpiece.cloud.system.dp.app.modular.biz.service.IDpTApiService;
import cn.jetpiece.cloud.system.dp.app.modular.biz.service.IDpTScopeDataService;
import cn.jetpiece.cloud.system.dp.app.modular.biz.wrapper.DpTApiWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * dp_t_api
 *
 * @author jetpiece
 * @since 2023-05-15
 */
@Slf4j
@RestController
public class DpTApiController extends BaseController<DpTApi, IDpTApiService> implements DpTApiApi {

    @Autowired
	private IDpTApiService dpTApiService;

    @Autowired
    private IDpTScopeDataService dpTScopeDataService;

    @Override
	public Result<String> add(CreateDpTApiParam param) {
	    try {
	        DpTApi dpTApi = BeanUtil.toBean(param, DpTApi.class);
            dpTApi.setDataStatus(DataStatusEnum.NORMAL.getValue());
	        if (dpTApiService.save(dpTApi)) {
                return Result.OK("添加成功!");
	        }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return Result.error("添加失败");
	}

	@Override
    public Result<String> update(UpdateDpTApiParam param) {
	    try {
	        DpTApi oldBean = dpTApiService.getById(param.getId());
	        if (oldBean == null) {
                return Result.error("数据未找到!");
	        }
	        DpTApi dpTApi = BeanUtil.toBean(param, DpTApi.class);
            dpTApi.setDataStatus(DataStatusEnum.NORMAL.getValue());
	        if (dpTApiService.updateById(dpTApi)) {
                return Result.OK("编辑成功!");
	        }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return Result.error("编辑失败");
	}

	@Override
	public Result<String> delete(Integer id) {
      try {
            if (dpTApiService.removeById(id)) {
                  return Result.OK("删除成功!");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return Result.error("删除失败");
	}

	@Override
	public Result<String> deleteBatch(String ids) {
      try {
            List<Integer> idList = Convert.toList(Integer.class, ids.split(","));
            if (dpTApiService.removeByIds(idList)) {
                  return Result.OK("批量删除成功!");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return Result.error("批量删除失败");
	}

	@Override
	public Result<DpTApiResult> get(Integer id) {
      try {
            DpTApi dpTApi = dpTApiService.getById(id);
            if (dpTApi == null) {
                return Result.error("数据未找到!");
            }
            return Result.OK(BeanUtil.toBean(dpTApi, DpTApiResult.class));
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return Result.error("获取数据失败！");
	}

	@Override
	public Result<PageResult<DpTApiResult>> page(DpTApiQuery query) {
	    DpTApi dpTApi = BeanUtil.toBean(query, DpTApi.class);
		QueryWrapper<DpTApi> queryWrapper = QueryGenerator.initQueryWrapper(dpTApi, HttpContext.getRequestParameters());
		if(StringUtils.isNotBlank(query.getAppKey())){
           DpTScopeData scopeData = dpTScopeDataService.getByAppKey(query.getAppKey());
            if (scopeData != null && StringUtils.isNotBlank(scopeData.getApiValue())) {
                List<String> apiList = new ArrayList<>();
                apiList.addAll(Arrays.asList(scopeData.getApiValue().split(",")));
                queryWrapper.notIn("id", apiList);
            }
        }
		Page<DpTApi> page = PageFactory.defaultPage();
		IPage<DpTApi> pageList = dpTApiService.page(page, queryWrapper);
        return Result.OK(new DpTApiWrapper(pageList).setWrapper(true).wrapPageResult());
	}

	@Override
    public Result<String> importExcel(MultipartHttpServletRequest request) {
        return super.importXls(request);
    }

	@Override
    public ModelAndView exportExcel(DpTApiQuery query) {
        return super.exportXls(query, DpTApiResult.class, "dp_t_api");
    }

}
